<?php
class Post extends AppModel {

	var $name = 'Post';
    var $primaryKey = 'post_id';
    var $actsAs = array('Containable');

    var $hasMany = array('Comment');
    var $belongsTo = array('Path','Category'=>array(
        'className'=>'Category',
        'counterCache'=>true,
        'counterScope'=>array('Post.status'=>'0','Post.status'=>'1'))
    );
    var $hasAndBelongsToMany = array('Tag');
    
    /**
     * Get Post data by Slug
     * usage in : Post controller -> function view()
     */
    function getPostBySlug($slug){
        $this->recursive = 0;
        $result = $this->find('first',array(
            'conditions'=>array('AND'=>array('Post.status'=>'1','Post.slug'=>$slug)),
            'fields'=>'Post.post_id,Post.title,Post.slug,Post.description,Post.image,Post.created,Post.view,Post.comment_count,Post.rate_count,Post.meta,Post.keyword',
            'contain'=>array(
                    'Path'=>array('fields'=>array('name')),
                    'Category'=>array('fields'=>array('name','slug')),
                    'Tag'=>array('fields'=>array('name','slug')),
            )
        ));

        return $result;
    }

    /**
     * Get next post by $id
     * usage in : Post controller -> function view()
     */
    function getNextPost($id){
        $this->recursive = -1;
        $result = $this->find('first',array(
            'conditions'=>array('AND'=>array('Post.status'=>'1','Post.post_id >'=>$id)),
            'fields'=>'Post.title,Post.slug'
        ));

        return $result;
    }

    /**
     * Get previous post by $id
     * usage in : Post controller -> function view()
     */
    function getPrevPost($id){
        $this->recursive = -1;
        $result = $this->find('first',array(
            'conditions'=>array('AND'=>array('Post.status'=>'1','Post.post_id <'=>$id)),
            'fields'=>'Post.title,Post.slug'
        ));

        return $result;
    }

    /**
     * Get 5 newest post
     * usage in : Post Controller -> function index()
     */
    function postOrderByDate(){
        $this->recursive = 0;
        $result = $this->find('all',array(
            'conditions'=>array('Post.status'=>'1'),
            'fields'=>'Post.title,Post.slug,Post.description,Post.image,Path.name',
            'order'=>'Post.post_id DESC',
            'limit'=>'5'
        ));

        return $result;
    }

    /**
     * Get 5 post ordered by View
     * usage in : Post Controller -> index()
     */
    function postOrderByView(){
        $this->recursive = 0;
        $result = $this->find('all',array(
            'conditions'=>array('Post.status'=>'1'),
             'fields'=>'Post.title,Post.slug,Post.image',
            'contain'=>array(
                'Path'=>array(
                    'fields'=>array('name')),
                'Tag'=>array(
                    'fields'=>array('name')),
                'Category'=>array(
                    'fields'=>array('name'))
                ),
            'order'=>'Post.view DESC',
            'limit'=>'5'
        ));

        return $result;
    }

    /**
     * Get 5 post ordered by Comments
     * usage in : Post Controller -> index()
     */
    function postOrderByComment(){
        $this->recursive = 0;
        $result = $this->find('all',array(
            'conditions'=>array('Post.status'=>'1'),
             'fields'=>'Post.title,Post.slug,Post.image,Post.comment_count,Path.name',
             'order'=>'Post.comment_count DESC',
            'limit'=>'5'
        ));

        return $result;
    }

    /**
     * Get 5 post ordered by Rates
     * usage in : Post Controller -> index()
     */
    function postOrderByRate(){
        $this->recursive = 0;
        $result = $this->find('all',array(
            'conditions'=>array('Post.status'=>'1'),
             'fields'=>'Post.title,Post.slug,Post.image',
            'contain'=>array(
                'Path'=>array(
                    'fields'=>array('name')),
                'Tag'=>array(
                    'fields'=>array('name')),
                'Category'=>array(
                    'fields'=>array('name'))
                ),
            'order'=>'Post.rate_count DESC',
            'limit'=>'5'
        ));

        return $result;
    }

    /**
     * Checking whether slug is exist or not
     * usage in : Rate Controller -> function add()
     */
    public function checkPostBySlug($slug){
        $this->recursive = -1;
        $result = $this->find('first',array(
            'fields'=>'Post.post_id',
            'conditions'=>array('AND'=>array('Post.status'=>'1','Post.slug'=>$slug))
        ));

        return $result;
    }


    /**
     * Check input title has exist or not in database
     * If same title exist, count same title and added result +1
     * This is makes slug different with same title
     * usage in :
     * Post Controller -> function savePost()
     */
    function checkTitle($title){
        $this->recursive = -1;
        $result = $this->find('all',array(
            'conditions'=>array('Post.title'=> $title),
            'fields'=>'Post.post_id'));

        if(!empty($result)) {
            $sum = count($result);
            $next = $sum+1;
            $result = $title.' '.$next;
        }	else {
            $result = $title;
        }

        return $result;
    }

    /**
     * Check same slug whether exist or not
     * If same slug exist, count same slug and added result +1 to slug
     * usage in :
     * Post Controller -> function savePost()
     */
    function checkSlug($slug) {
        $this->recursive = -1;

        $result = $this->find('all',array(
            'conditions'=>array('Post.slug'=> $slug),
            'fields'=>'Post.post_id'));

        if(!empty($result)) {
            $sum = count($result);
            $next = $sum+1;
            $result = $slug.'_'.$next;
        }	else {
            $result = $slug;
        }

        return $result;
    }

    /**
     * Get related Post by array post_id
     * usage in Post Controller -> function view()
     */
    function getRelatedPost($post_id){
        $this->recursive = 0;
        $result = $this->find('all',array(
            'conditions'=>array('AND'=>array(
                    'Post.post_id'=>$post_id,
                    'Post.status'=>'1',
                )),
            'fields'=>'Post.title, Post.slug, Post.created,Path.name,Post.image'
        ));

        return $result;
    }

    /**
     * Get Post info for catalog update
     * usage in Post Controller -> function saveDraft(), savePost()
     */
    function getPostForCatalog($post_id){
        $this->recursive = 0;
        $result = $this->find('first',array(
            'conditions'=>array('AND'=>array('Post.post_id'=>$post_id)),
            'fields'=>'Post.title,Post.category_id,Post.description',
            'contain'=>array(
                    'Category'=>array('fields'=>array('name')),
                    'Tag'=>array('fields'=>array('name')),
            )
        ));
    
        return $result;
    }

    /**
     * Get random post
     * usage in users controller -> function about()
     */
    function getRandomPost(){
        $this->recursive = 0;
        $result = $this->find('all',array(
            'conditions'=>array('Post.status'=>'1'),
             'fields'=>'Post.title,Post.slug,Post.image',
            'contain'=>array(
                'Path'=>array(
                    'fields'=>array('name')),
                'Tag'=>array(
                    'fields'=>array('name')),
                'Category'=>array(
                    'fields'=>array('name'))
                ),
            'order'=>'RAND() ',
            'limit'=>'5'
            ));

       return $result;
    }

    /**
     * Get Post RSS
     * usage in Sitemapst Controller -> function freelogo()
     */
     public function getFeed(){
         $this->recursive = 0;
        $result = $this->find('all',array(
            'conditions'=>array('Post.status'=>'1'),
             'fields'=>'Post.title,Post.slug,Post.image,Post.description,Path.name,Post.created,Post.comment_count',
             'order'=>'Post.created DESC',
            'limit'=>'20'
            ));

        return $result;
     }

     /**
      * Get sitemaps
      * usage in Sitemaps Controller -> Sitemaps ()
      */
     function getPostForSitemap(){
        $this->recursive = -1;
        $result = $this->find('all',array(
            'conditions'=>array('Post.status'=>'1'),
             'fields'=>'Post.slug',
             'order'=>'Post.created DESC',
            ));

        return $result;
     }

     /**
      * Get Post_id by Image
      * usage in Sitemaps Controller -> Sitemaps ()
      */
     function getPostByImage($image){
        $this->recursive = -1;
        $result = $this->find('first',array(
            'conditions'=>array('AND'=>array('Post.status'=>'1','Post.image'=>$image)),
             'fields'=>'Post.post_id',
            ));

        return $result;
     }

    /**
     * Pagination search result
     * Posts controller -> search()
     */
    function paginateSearch($id = array())
    {
        $this->recursive = 0;
        $list = implode(",",$id);
        $order = 'FIELD(Post.post_id, '.$list.')';

        $result = array(
               'fields'=>'Post.post_id,Post.category_id,Post.slug,Post.title,Post.description,Post.image',
                'contain'=>array(
                    'Path'=>array(
                        'fields'=>array('name')),
                    'Tag'=>array(
                        'fields'=>array('name')),
                    'Category'=>array(
                        'fields'=>array('name'))
                    ),
                'limit' => 10,
                'order' => $order
        );

        return $result;
    }

}

?>